package com.kye.base.utils;

import com.kye.base.Logger;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/12/2017
 * @updated : 29/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class DateUtils {

    public static Calendar dateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar dateStringToCalendar(String dateString) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar dateStringToCalendarYMD(String dateString) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar dateStringToCalendar(String dateString, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static String getDateStringByDate(Date date, String format) {
        if (date != null){
            DateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(date);
        }
        return "";
    }

    public static String getDateStringByDate(Date date) {
        if (date != null){
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.format(date);
        }
        return "";
    }

    public static String getDateStringByDateYearMonthDay(Date date) {
        if (date != null){
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return dateFormat.format(date);
        }
        return "";
    }

    public static String getDateStringByDateHourMin(Date date) {
        if (date != null){
            DateFormat dateFormat = new SimpleDateFormat("HH:mm");
            return dateFormat.format(date);
        }
        return "";
    }

    public static String getDateStringByDateHourMinSecond(Date date) {
        if (date != null){
            DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            return dateFormat.format(date);
        }
        return "";
    }

    public static long getAdvertiseTimeStamp() {
        Calendar instance = Calendar.getInstance();
        String minFormat = "00";
        int min = instance.get(Calendar.MINUTE);
        if (0 < min && min < 30) {
            min = 0;
        } else {
            min = 30;
        }
        instance.set(Calendar.MINUTE, min);
        instance.set(Calendar.SECOND, 0);
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        int day = instance.get(Calendar.DAY_OF_MONTH);
        int min_ = instance.get(Calendar.MINUTE);
        int sec = instance.get(Calendar.SECOND);
//        Logger.d("year : " + year + " , month : " + month + " , day : " + day + " , min " + min_ + " , sec : " + sec);
        return instance.getTimeInMillis() / 1000;
    }

    public static long getSimpleLongTime(String time) {
        long millionSeconds;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            millionSeconds = sdf.parse(time).getTime();//毫秒
            return millionSeconds;
        } catch (Exception e) {
            Logger.e(e, "getSimpleLongTime");
        }
        return 0;
    }

    public static Boolean testLeftLessOrEqualToRight(Calendar calendarFrom, Calendar calendarTo) {
        int yearFrom = calendarFrom.get(Calendar.YEAR);
        int dayFrom = calendarFrom.get(Calendar.DAY_OF_YEAR);
        int yearTo = calendarTo.get(Calendar.YEAR);
        int dayTo = calendarTo.get(Calendar.DAY_OF_YEAR);
        if (yearFrom > yearTo) {
            return false;
        } else if (yearFrom == yearTo && dayFrom > dayTo) {
            return false;
        } else {
            return true;
        }
    }
}
